<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item style="width: 100%" label="项目名称" prop="name">
            <el-input v-model="form.name" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="ctm_id" style="width: 100%">
            <CustomerSelect v-model="form.ctm_id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报价人" prop="su_quoted" style="width: 100%">
            <StaffSelect v-model="form.su_quoted" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="成本" prop="cost" style="width: 100%">
            <el-input v-model="form.cost" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报价金额" prop="quoted_price" style="width: 100%">
            <el-input v-model="form.quoted_price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="议价金额" prop="negotiated_price" style="width: 100%">
            <el-input v-model="form.negotiated_price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单金额" prop="trading_price" style="width: 100%">
            <el-input v-model="form.trading_price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark" style="width: 100%">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { updPro, addPro } from '@/api/project'
import CustomerSelect from '@/myComponents/CustomerSelect'
import StaffSelect from '@/myComponents/StaffSelect'

export default {
  components: { CustomerSelect, StaffSelect },
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      customers: [],
      staffs: [],
      form: {},
      rules: {
        name: [{ required: true, trigger: 'blur', message: '客户名称不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addPro(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updPro(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
